package com.hk.core.authentication.api.validatecode;

import com.hk.commons.util.IDGenerator;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 验证码
 *
 * @author kevin
 * @date 2018-07-27 13:40
 */
@Getter
@Accessors(chain = true)
public class ValidateCode implements Serializable {

    @Setter
    private String id;

    /**
     * 验证码
     */
    @Setter
    private String code;

    /**
     * 过期时间秒,单位:秒
     */
    private int expireSecond;

    /**
     * 过期时间
     */
    private LocalDateTime expireTime;

    public ValidateCode() {

    }

    public ValidateCode(String code, int expireSecond) {
        this(IDGenerator.uuid32().generate(), code, expireSecond);
    }

    public ValidateCode(String id, String code, int expireSecond) {
        this.id = id;
        this.code = code;
        this.setExpireSecond(expireSecond);
    }

    public ValidateCode setExpireSecond(int expireSecond) {
        this.expireSecond = expireSecond;
        this.expireTime = LocalDateTime.now().plusSeconds(expireSecond);
        return this;
    }

    /**
     * 验证码是否过期
     *
     * @return true if expire
     */
    public boolean isExpired() {
        return expireTime != null && LocalDateTime.now().isAfter(expireTime);
    }
}
